<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\Admin as AdminModel;
use app\lib\tool\Str;
use think\facade\Cache;

class Admin extends BaseController
{
    public function list()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $page = input('page', 1, 'intval');
        $perpage = 10;
        $result = AdminModel::page($page, $perpage)->select();
        $total = AdminModel::count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(200, '管理员列表', $data);
    }

    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $username = input('username', '');
        $password = input('password', '');
        $nickname = input('nickname', '');
        $result = AdminModel::create([
            'username' => $username,
            'password' => md5($password . $username),
            'nickname' => $nickname
        ]);
        if ($result) {
            return show(200, '添加成功');
        } else {
            return show(config('status.error_add'), '添加失败');
        }
    }

    public function edit()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $data = AdminModel::where('id', $id)->find()->toArray();
        return show(200, '查询成功', $data);
    }

    public function modify()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $username = input('username', '');
        $password = input('password', '');
        $nickname = input('nickname', '');

        $one = AdminModel::where('id', $id)->find()->toArray();
        if ($password == $one['password']) {
            $result = AdminModel::update([
                'id' => $id,
                'username' => $username,
                'nickname' => $nickname
            ]);
        } else {
            $result = AdminModel::update([
                'id' => $id,
                'username' => $username,
                'password' => md5($password . $username),
                'nickname' => $nickname
            ]);
        }
        if ($result) {
            return show(200, '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    public function delete()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        if ($id == 1) {
            return show(config('status.super_admin_delete'), '超级管理员不能删除');
        }

        $result = AdminModel::destroy($id);
        if ($result) {
            return show(200, '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }

    public function login()
    {
        $username = input('username', '');
        $password = input('password', '');

        $password = md5($password . $username);
        $result = AdminModel::where(['username' => $username, 'password' => $password])->find();
        if ($result) {
            $token = Str::getToken('');
            $redisData = [
                'id' => $result['id'],
                'username' => $result['username']
            ];
            Cache::set(config('cache.redis.prefix') . $token, $redisData, 3600 * 12);

            $data = ['token' => $token, 'nickname' => $result['nickname']];
            return show(200, '登录成功', $data);
        } else {
            return show(config('status.admin_login_error'), '账号信息错误，请重试');
        }
    }

    public function logout()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        Cache::delete(config('cache.redis.prefix') . $this->request->token);
        return show(200, '退出登录');
    }
}
